package com.jml.单例模式.反射破坏内部类;

import com.jml.单例模式.双重检查;

import java.lang.reflect.Constructor;

public class 反射破坏单例 {
    public static void main(String[] args) throws Exception {
        // 使用反射机制创建我们的对象
        Class<?> aClass = Class.forName("com.jml.单例模式.反射破坏内部类.LazyInnerClassSingleton");
        Constructor<?> constructor = aClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        // 走无参构造函数 反射创建对象成功
        LazyInnerClassSingleton instance1 = (LazyInnerClassSingleton) constructor.newInstance();
        LazyInnerClassSingleton instance = instance1.getInstance();
        LazyInnerClassSingleton instance2 = (LazyInnerClassSingleton) constructor.newInstance();
        System.out.println(instance == instance2);
        System.out.println(instance == instance1);
        System.out.println(instance1 == instance2);
    }

}
